
const fs = require("fs");
const path = require('path');

async function sleep(tick){
  return new Promise((resolve,reject)=>{
    setTimeout(()=>{
      resolve('done');
    }, tick);
  })
}

// async function getStat(path){
//   return new Promise((resolve, reject) => {
//     fs.stat(path, (err, stats) => {
//       if(err){
//         resolve(false);
//       }else{
//         resolve(stats);
//       }
//     })
//   })
// }

// async function exists(path) {
//   return new Promise((resolve, reject) => {
//     fs.exists(path, (err, exists) => {
//       if(err){
//         resolve(false);
//       }else{
//         resolve(exists);
//       }
//     })
//   })
// }

//
// function mkdirsSync(dirname) {
//   if (fs.existsSync(dirname)) {
//     return true;
//   } else {
//     if (mkdirsSync(path.dirname(dirname))) {
//       fs.mkdirSync(dirname);
//       return true;
//     }
//   }
// }

async function mkdir(dirName){
  if (fs.existsSync(dirName)) {
    return true;
  } else {
    if (mkdir(path.dirname(dirName))) {
      fs.mkdirSync(dirName);
      return true;
    }
  }
}

async function clearPDFFolder(dirName) {
  const arr = fs.readdirSync(dirName);
  const today = new Date().format('yyyy_MM_dd');
  (arr || []).forEach(name => {
    if (name !== today) {
      clearFolder(path.join(dirName, name), true);
    }
  })
}

async function clearFolder(dirName, removeSelf) {
  const stat = fs.statSync(dirName);
  if (stat.isFile()) {
    fs.unlinkSync(dirName);
  } else {
    const arr = fs.readdirSync(dirName);
    (arr || []).forEach(name => {
      const curPath = path.join(dirName, name);
      clearFolder(curPath, true);
    });
    if (removeSelf) {
      fs.rmdirSync(dirName);
    }
  }
}

// async function readFile(filePath){
//   return new Promise(async (resolve,reject)=>{
//     try {
//       fs.readFile(filePath, "utf-8", function(err, data) {
//       if (err) {
//         reject('error');
//       } else {
//         resolve(data);
//       }
//     });
//   } catch (ex) {
//       reject('error');
//     }
//   })
// }


function uuid () {
  return new Date().format('yyyyMMddhhmmssS') + Math.random().toString().substr(-5);
}

Array.prototype.each = async function(fn) {
  let len = this.length;
  for (let i = 0; i < len; ++i) {
    await fn.call(fn, this[i]);
  }
};

Date.prototype.format = function (format) {
  var o = {
    "M+": this.getMonth() + 1, //month
    "d+": this.getDate(), //day
    "h+": this.getHours(), //hour
    "m+": this.getMinutes(), //minute
    "s+": this.getSeconds(), //second
    "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
    "S": this.getMilliseconds() //millisecond
  };

  if (/(y+)/.test(format)) {
    format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  }

  for (var k in o) {
    if (new RegExp("(" + k + ")").test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    }
  }
  return format;
};

function getCurTimestamp() {
  return new Date().format('yyyy-MM-dd hh:mm.ss');
}

function getLeftRightMargin(margin) {
  if (margin) {
    return parseFloat(margin.left || 0) + parseFloat(margin.right || 0);
  } else {
    return 0;
  }
}

module.exports = {
  sleep,
  mkdir,
  clearPDFFolder,
  uuid,
  getCurTimestamp,
  getLeftRightMargin
};